#!/usr/bin/env bash

set -e

DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && cd "../" && pwd)"

echo start build image...

# 修改 Docker Registry 配置
DOCKER_REGISTRY="crpi-w5ssdu2dhix3pbdl.cn-hangzhou.personal.cr.aliyuncs.com"
DOCKER_IMAGE_NAME="qwlabs/crp-server"
APP_BASE_VERSION="$(cat ${DIR}/.version)"

if [ -z "${PIPELINE_ID}" ];then
	APP_VERSION=${APP_BASE_VERSION}.dev
else
  APP_VERSION=${APP_BASE_VERSION}.${PIPELINE_ID}
fi

echo start build image version:"${APP_VERSION}"...

DOCKER_BUILDKIT=1 docker build "${DIR}" -f app/src/main/docker/Dockerfile \
  -t ${DOCKER_IMAGE_NAME}:"${APP_VERSION}"


if [ "${PIPELINE_ID}" ];then
    echo "${DOCKER_LOGIN_PASSWORD}" | docker login --username="aliyun9690088038 " --password-stdin ${DOCKER_REGISTRY}
    docker push ${DOCKER_IMAGE_NAME}:${APP_VERSION}
fi
